<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <el-page-header :content="id?'编辑企业':'增加企业'" @back="$router.back()" />
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="addForm" label-width="100px" :model="addForm" :rules="formRules">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode" >
              <el-option
                v-for="item in options"
                :key="item.industryCode"
                :label="item.industryName"
                :value="item.industryCode">
              </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseId" />
            <!-- action不太灵活，基本不用，但是又必须用，所以用#代替路径值 -->
            <el-upload
              class="upload-demo"
              action="#"
              :http-request="upload"
              :before-upload="beforeUpload">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
</el-upload>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="confirmForm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getindustryAPI,editParkAPI,addParkAPI,getEditAPI} from '@/api/park'
import {uploadAPI} from '@/api/common'
export default {
  data() {
    const vphone=function(rule,val,callback){
      const reg=/^1[3-9]\d{9}$/
      if(reg.test(val)){
        callback()
      }else{
        callback(new Error('输入的手机号码有误'))
      }
    }
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },

      options: [],
      value:'',
      formRules:{
        name:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'}
        ],
        legalPerson:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'}
        ],
        registeredAddress:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'}
        ],
        industryCode:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'}
        ],
        contact:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'}
        ],
        contactNumber:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'},
          {validator:vphone,trigger:'blur'}
        ],
        // businessLicenseUrl:[
        //   {required:true,message:'必填项，内容不能为空',trigger:'change'}
        // ],
        businessLicenseId:[
          {required:true,message:'必填项，内容不能为空',trigger:'change'}
        ]
      }
    }
  },
  computed:{
    id(){
      return this.$route.query.id
    }
  },
  mounted(){
      this.getindustryList()
      if(this.id){
        this.getEditList()
      }
  },
  methods:{
    async getEditList(){
      const res= await getEditAPI(this.id)
      console.log(res)
      const {id,name,legalPerson,registeredAddress,industryCode,contact,contactNumber,businessLicenseId,businessLicenseUrl}=res.data
      this.addForm={id,name,legalPerson,registeredAddress,industryCode,contact,contactNumber,businessLicenseId,businessLicenseUrl}
    },
    beforeUpload(file){
      // console.log(file)
      var imageType=["image/png","image/jpg"]
      var isPNGorJPG=imageType.includes(file.type)
      var isSize=file.size/1024/1024<5
      if(!isPNGorJPG){
        this.$message.error("上传文件格式不符合")

      }
      if(!isSize){
        this.$message.error("上传的图片不超过5M")
      }
      return isPNGorJPG && isSize
    },
    async upload(data){
      // console.log(data)
      const {file}=data
      const formData=new FormData()
      //追加file和type到formData实例中
      formData.append('file',file)
      formData.append('type',1)
      const res=await uploadAPI(formData)
      // console.log(res)
      this.addForm.businessLicenseId=res.data.id
      this.addForm.businessLicenseUrl=res.data.url

    },
    async getindustryList(){
      const res=await getindustryAPI()
      this.options=res.data
      // console.log(res)
    },
    confirmForm(){
      this.$refs.addForm.validate(async(flag)=>{
            if(flag){
              //开始调用接口添加数据
              const FormData={
                ...this.addForm,
              }
              if(this.id){
                await editParkAPI(FormData)
              }else{
                await addParkAPI(FormData)

              }

              this.$message.success(`${this.id?'更新成功':'添加成功'}`)
             this.$router.back()
            }


      })
    },
    resetForm(){
      this.$refs.addForm.resetFields()
    }

  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left:20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form{
          display: flex;
          flex-wrap: wrap;
          .el-form-item{
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
